<?
/**
 * @id $Id: goodsStockManager.php 11 2007-06-19 07:10:13Z gamezoom $
 * @author $Author: gamezoom $
 * @version $Revision: 11 $
 * @date $Date: 2007-06-19 07:10:13 +0000 (Tue, 19 Jun 2007) $
 * @svn $HeadURL: http://jxcfortaobao.googlecode.com/svn/trunk/core/www/goodsStockManager.php $
 */

L2P::using('inc.abstractWwwModule');

class goodsStockManager extends abstractWwwModule
{
	function defaultEvent()
	{
		return $this->showManagerForm();
	}

	function showManagerForm()
	{
		return self::display("goodsStockManagerForm");
	}

	function showRejectEditForm()
	{
		return self::display("goodsStockRejectEditForm");
	}

	/**
	 *
	 */
	function stock()
	{
		$id = isset($_POST["g_id"]) ? $_POST["g_id"] + 0 : 0;
		$price = isset($_POST["g_price"]) ? $_POST["g_price"] + 0 : 0;
		$count = isset($_POST["g_count"]) ? $_POST["g_count"] + 0 : 0;
		$postage = isset($_POST["g_postage"]) ? $_POST["g_postage"] + 0 : 0;

		$errors = array();

		if($price < 0.01){
			$errors["g_price"] = "g_price is error";
		}
		if($id < 1){
			$errors["g_id"] = "g_id is error";
		}else{
			$goods = pdoFactory::fetch("SELECT * FROM goods WHERE id={$id}");

			if(!$goods){
				$errors["g_id"] = "g_id is unknown" . $id;
			}
		}
		if($count < 1){
			$errors["g_count"] = "g_count is error";
		}

		if(count($errors) == 0){
			$now = date("Y-m-d H:i:s");

			//插入进货批次表
			pdoFactory::exec("INSERT INTO goods_stock(stock_g_id, stock_price, stock_count, stock_date) VALUES(:g_id, :g_price, :g_count, '{$now}')", array(":g_id" => $id, ":g_price" => $price, ":g_count" => $count));

			//获得批次编号
			$last_insert_data = pdoFactory::fetch("SELECT LAST_INSERT_ID() AS id");
			$last_insert_id = $last_insert_data["id"];

			//插入进货邮资表
			if($postage > 0){
				pdoFactory::exec("INSERT INTO fees(fee_type, relation_id, fee_amount, fee_date) VALUES(:fee_type, :relation_id, :fee_amount, '{$now}')", array(":fee_type" => FEE_TYPE_OF_STOCK_POSTAGE, ":relation_id" => $last_insert_id, ":fee_amount" => $postage));
			}

			$this->logEvent("goodsStockManager", "goods stock:\t{$id}\t{$price}\t{$count}\t{$postage}");

			header("Location: ?module=goodsStockManager");
			exit();
		}

		self::assign("errors", $errors);
		return $this->showManagerForm();
	}

	function reject()
	{
		$id = isset($_POST["g_id"]) ? $_POST["g_id"] + 0 : 0;
		$sum = isset($_POST["g_sum"]) ? $_POST["g_sum"] + 0 : 0;
		$count = isset($_POST["g_count"]) ? $_POST["g_count"] + 0 : 0;

		$errors = array();

		if($sum < 0.01){
			$errors["g_sum"] = "g_sum is error";
		}
		if($id < 1){
			$errors["g_id"] = "g_id is error";
		}else{
			$goods = pdoFactory::fetch("SELECT * FROM goods WHERE id={$id}");

			if(!$goods){
				$errors["g_id"] = "g_id is unknown" . $id;
			}
		}
		if($count < 1){
			$errors["g_count"] = "g_count is error";
		}else{
			$goods = pdoFactory::fetch("SELECT * FROM goods_valid_count WHERE g_id = {$id} AND g_count >= {$count}");
			if(!$goods){
				$errors["g_count"] = "goods count is not enough";
			}
		}

		if(count($errors) == 0){
			pdoFactory::exec("INSERT INTO goods_reject(g_id, g_sum, g_count, g_date) VALUES(:g_id, :g_sum, :g_count, NOW())", array(":g_id" => $id, ":g_sum" => $sum, ":g_count" => $count));

			$this->logEvent("goodsStockManager", "goods reject:\t{$id}\t{$sum}\t{$count}");

			header("Location: ?module=goodsStockManager&event=showRejectEditForm");
			exit();
		}

		self::assign("errors", $errors);
		return $this->showRejectEditForm();
	}

	function showStockList()
	{
		return self::display("goodsStockListForm");
	}
}
?>